package com.ap.core.bulkupload.translator;

import java.util.UUID;

import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ap.core.cxf.jaxb.managemerchant.RegisterMerchantMessageType;
import com.ap.core.cxf.jaxb.managemerchant.RegisterMerchantMsgHdrType;
import com.ap.core.cxf.jaxb.msghdr.MsgHdrType;
import com.ap.core.exception.APCoreException;

/**
 * The Class MsgHdrTypeBuilder.
 *
 * @author Siva.Pochana
 */
public class MsgHdrTypeTranslator {

	/** The Constant LOG. */
	private final static Logger LOG = LoggerFactory.getLogger(MsgHdrTypeTranslator.class);
	
	/**
	 * Builds the msg hdr type.
	 *
	 * @param msgType the msg type
	 * @return the msg hdr type
	 * @throws APCoreException the aP core exception
	 */
	public static RegisterMerchantMsgHdrType buildMsgHdrType(RegisterMerchantMessageType msgType) throws APCoreException {
		String methodName = "buildMsgHdrType() - ";
		try {
			RegisterMerchantMsgHdrType msgHdrType = new RegisterMerchantMsgHdrType();
			msgHdrType.setMsgId(MsgHdrTypeTranslator.getMsgId());
			msgHdrType.setMsgType(msgType);
			msgHdrType.setMsgTimestamp( new LocalDateTime());
			
			return msgHdrType;
		} catch (Exception ex) {
			LOG.error(methodName + "Exception occurred :"+ex.getMessage(), ex);
			throw new APCoreException(ex);
		}
	}
	
	/**
	 * Gets the msg id.
	 *
	 * @return the msg id
	 */
	private static String getMsgId() {
		UUID uuid = UUID.randomUUID();
		String msgId = uuid.toString();
		if(msgId.length() > 35) {
			msgId = msgId.substring(0, 35);
		}
		
		return msgId;
	}

}
